package com.bestcem.xm.user.entity.base;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 数据库分页器
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/4/26 15:57
 */
@Getter
@Setter
@NoArgsConstructor
public class Paginator {

    /**
     * 默认排序：更新时间
     */
    private static final String ORDER_BY_CLAUSE_DEFAULT = "create_time desc";

    /**
     * 当前页
     */
    private Integer currentPage;


    /**
     * 分页大小
     */
    private Integer itemsPerPage;

    /**
     * 偏移量
     */
    private Integer offset;

    /**
     * 排序
     */
    private String orderByClause;

    /**
     * 构造器
     *
     * @param currentPage 当前页
     * @param pageSize 分页大小
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/4/26 15:57
     */
    public Paginator(Integer currentPage, Integer pageSize) {
        this.currentPage = currentPage;
        this.itemsPerPage = pageSize;
        this.orderByClause = ORDER_BY_CLAUSE_DEFAULT;
    }

    /**
     * 通过当前页和分页大小获取偏移量
     *
     * @return Integer
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/4/26 15:56
     */
    public Integer getOffset() {
        return (this.currentPage - 1) * this.itemsPerPage;
    }
}
